#!/bin/sh

os=$(uname -s)

if [[ "$os" == "Darwin" ]]; then
	host_tag="darwin-x86_64"
elif [[ "$os" == "CYGWIN"* ]]; then
	host_tag="windows-x86_64"
else
	host_tag="linux-x86_64"
fi

PROJECT=xloop_basic
RUNNER_DIR=../../runner
TARGET_DIR=../../

if [[ "$1" = "ios" ]]; then
	PROFILE=dev
	PROFILE_DIR=debug
	cargo build \
		--target aarch64-apple-ios \
		-p $PROJECT \
		--profile $PROFILE \
		--example ios
	cargo build \
		--target x86_64-apple-ios \
		-p $PROJECT \
		--profile $PROFILE \
		--example ios

	lipo -create -output $RUNNER_DIR/ios/mudx/libmudx.a \
		$TARGET_DIR/target/x86_64-apple-ios/$PROFILE_DIR/examples/libios.a \
		$TARGET_DIR/target/aarch64-apple-ios/$PROFILE_DIR/examples/libios.a
	lipo -archs $RUNNER_DIR/ios/mudx/libmudx.a
fi


if [[ "$1" = "iosapp" ]]; then
	PROFILE=release
	PROFILE_DIR=release
	# PROFILE=dev
	# PROFILE_DIR=debug
	TARGET=aarch64-apple-ios
	cargo build --target $TARGET -p $PROJECT --profile $PROFILE 

	cp 	$TARGET_DIR/target/$TARGET/$PROFILE_DIR/$PROJECT $RUNNER_DIR/ios.app/xloop
	# xcrun simctl boot "iPhone 16 Pro"
	# open -a Simulator
	# xcrun simctl install booted $RUNNER_DIR/ios.app/
	# xcrun simctl launch --console booted com.mindbox.xloop
fi

if [[ "$1" = "mac" ]]; then
	PROFILE=dev
	PROFILE_DIR=debug
	cargo build \
		--target aarch64-apple-darwin \
		--profile $PROFILE \
		-p $PROJECT \
		--example macos
	cargo build \
		--target x86_64-apple-darwin \
		--profile $PROFILE \
		-p $PROJECT \
		--example macos

	lipo -create -output $RUNNER_DIR/macos/mudx/libmudx.a \
		$TARGET_DIR/target/x86_64-apple-darwin/$PROFILE_DIR/examples/libmacos.a \
		$TARGET_DIR/target/aarch64-apple-darwin/$PROFILE_DIR/examples/libmacos.a
	lipo -archs $RUNNER_DIR/macos/mudx/libmudx.a
fi

if [[ "$1" = "macapp" ]]; then
	PROFILE=release
	PROFILE_DIR=release

	TARGET=aarch64-apple-darwin
	cargo build \
		--target $TARGET \
		--profile $PROFILE \
		-p $PROJECT \

	mkdir $RUNNER_DIR/mac.app/Contents/MacOs
	cp 	$TARGET_DIR/target/$TARGET/$PROFILE_DIR/$PROJECT \
		$RUNNER_DIR/mac.app/Contents/MacOs/mudx
	open $RUNNER_DIR/mac.app
fi

if [[ "$1" = "win" ]]; then
	PROFILE=dev
	PROFILE_DIR=debug

	TARGET=x86_64-pc-windows-gnu
	cargo build \
		-p $PROJECT \
		--profile $PROFILE \
		--target $TARGET

	cp	$TARGET_DIR/target/x86_64-pc-windows-gnu/$PROFILE_DIR/$PROJECT.exe \
		$RUNNER_DIR/windows/$PROJECT.exe
	wine $RUNNER_DIR/windows/$PROJECT.exe
fi

if [[ "$1" = "ndk" ]]; then
	PROFILE=dev
	PROFILE_DIR=debug

	TARGET=aarch64-linux-android
	cargo build --profile $PROFILE --target $TARGET -p $PROJECT --example android
	cp	$TARGET_DIR/target/$TARGET/$PROFILE_DIR/examples/libandroid.so \
		$RUNNER_DIR/android/app/src/main/jniLibs/arm64-v8a/libxloop.so

	# TARGET=x86_64-linux-android
	# cargo build --profile $PROFILE --target $TARGET -p $PROJECT --example android
	# cp 	$TARGET_DIR/target/$TARGET/$PROFILE_DIR/examples/libandroid.so \
	# 	$RUNNER_DIR/android/app/src/main/jniLibs/x86_64/libxloop.so
fi

if [[ "$1" = "avd" ]]; then
	emulator @Api33 -gpu host
fi

if [[ "$1" = "web" ]]; then
	PROFILE=release
	PROFILE_DIR=release

	TARGET=wasm32-unknown-unknown
	cargo build \
		--profile $PROFILE \
		--target $TARGET \
		-p $PROJECT \
		--example web

	wasm-bindgen --target web --no-typescript --omit-default-module-path \
		--out-dir $RUNNER_DIR/web/ \
		--out-name xloop \
		$TARGET_DIR/target/$TARGET/$PROFILE_DIR/examples/web.wasm 
fi
